<?php
session_start();

define('ROOT', dirname(dirname(__FILE__)) . '/');
include_once( ROOT . 'weibo/config.php' );
include_once( ROOT . 'weibo/saetv2.ex.class.php' );
include_once( ROOT . 'weibo/func.php' );
include_once( ROOT . 'weibo/database.php' );

$db = db_weibo();
if ($db === false) exit('DB Error!');

$uid = 1346818450;
$appkey = WB_AKEY;
$sql = "select uid, access_token, update_time from token where uid={$uid} and appkey={$appkey};";
$re = $db->query($sql);
$token = $re[0]['access_token']; // error_code:21332 error:invalid_access_token
$c = new SaeTClientV2( WB_AKEY , WB_SKEY , $token );

$db->closs();

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>新浪微博V2接口演示程序-Powered by Sina App Engine</title>
</head>

<body>
<?php
$page = 1;
$size = 20;
$favorite  = $c->get_favorites($page, $size);
$total = $favorite['total_number'];
if ($total > 0){
	$fav_arr = $favorite['favorites'];
	foreach ($fav_arr as $fav){
        $contents = conv_time($fav['favorited_time']);
        if (!empty($fav['tags'])){
            foreach ($fav['tags'] as $tag){
                $contents .= " {$tag['id']}.{$tag['tag']}({$tag['count']})";
            }
        }
        $contents .= '<br />';

        if (isset($fav['status']['deleted'])){
        	$contents .= add_url_href($fav['status']['text']);
        }else{
	        if (isset($fav['status']['retweeted_status'])){
	            $contents .= "<strong><a href=\"http://weibo.com/u/{$fav['status']['retweeted_status']['user']['id']}\" target=\"_blank\">{$fav['status']['retweeted_status']['user']['screen_name']}</a>：</strong>";
	            $contents .= add_url_href($fav['status']['retweeted_status']['text']);
	            $contents .= '<br/><strong>转发：</strong>';
	            $contents .= "<a href=\"http://weibo.com/u/{$fav['status']['user']['id']}\" target=\"_blank\">{$fav['status']['user']['screen_name']}</a>：";
	            $contents .= add_url_href($fav['status']['text']);
	        }else{
	            $contents .= "<strong><a href=\"http://weibo.com/u/{$fav['status']['user']['id']}\" target=\"_blank\">{$fav['status']['user']['screen_name']}</a>：</strong>";
	            $contents .= add_url_href($fav['status']['text']);
	        }
		    if (isset($fav['status']['retweeted_status']['thumbnail_pic'])){
	        	$contents .= "<br/><br/><a href=\"{$fav['status']['retweeted_status']['original_pic']}\" target=\"_blank\"><img src=\"{$fav['status']['retweeted_status']['thumbnail_pic']}\"/></a>";
	        }
        }
        
		echo '<div style="padding:10px;margin:5px;border:1px solid #ccc">', $contents, '</div>', "\n";
	}
}else{
	echo '<div style="padding:10px;margin:5px;border:1px solid #ccc">无内容！</div>';
}

?>

</body>
</html>